package com.yww.datastructure.line;

import java.util.function.Consumer;
import java.util.stream.Stream;

/**
 * @author yww
 * @description
 * @since 2024/1/30 14:03
 */
public interface Linear<E> extends Iterable<E> {
    E get(int index);

    void addLast(E e);

    void addFirst(E e);

    void add(E e, int index);

    void foreach(Consumer<E> consumer);

    Stream<E> stream();

    E remove(int index);

    E removeLast();

    E removeFirst();

    int size();

    boolean isEmpty();


    default String asString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        foreach(val -> {
            stringBuilder.append(val);
            stringBuilder.append(", ");
        });
        if (!isEmpty()) {
            stringBuilder.deleteCharAt(stringBuilder.lastIndexOf(" "));
            stringBuilder.deleteCharAt(stringBuilder.lastIndexOf(","));
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }

    default IllegalArgumentException illegalIndex(int index) {
        return new IllegalArgumentException(String.format("index [%d] 不合法！%n", index));
    }
}
